Ext.define('wmf.controller.access.access', {
    extend: 'Ext.app.Controller',
    stores: ['access.access',
             'access.ecId',
             'access.protocalType'
    ],
    models: ['access.access'],
    views: ['access.main', 
            'access.list', 
            'access.east', 
            'access.add', 
            'access.edit', 
            'access.query'
    ],
    
    refs: [{
        ref: 'accessList',
        selector: 'accessList'
    },{
    	ref: 'accessMain',
        selector: 'accessMain'
    },{
        ref: 'accessQuery',
        selector: 'accessQuery'
    },{
        ref: 'accessAdd',
        selector: 'accessAdd'
    },{
        ref: 'accessEdit',
        selector: 'accessEdit'
	},{
        ref: 'accessEast',
        selector: 'accessEast'
    }],
    
    init: function () {
        this.control({
        	'accessList': {
        		beforerender: this.onBeforeRender,
        		celldblclick: this.onCellDblClick
            },
            'accessList button#add': {
                click: this.onAddButtonClick
            },
            'accessList button#query': {
                click: this.onQueryButtonClick
            },
            'accessList button#export': {
                click: this.onExportButtonClick
            },
			'accessEast tool#minimize': {
				click: this.onMinToolClick
			},
			'accessAdd button#save': {
				click: this.onAddSaveButtonClick
			},
			'accessAdd button#reset': {
				click: this.onAddResetButtonClick
			},
			'accessAdd button#cancel': {
				click: this.onMinToolClick
			},
			'accessEdit button#save': {
				click: this.onEditSaveButtonClick
			},
			'accessEdit button#reset': {
				click: this.onEditResetButtonClick
			},
			'accessEdit button#cancel': {
				click: this.onMinToolClick
			},
        	'accessQuery button#save': {
				click: this.onQuerySaveButtonClick
			},
			'accessQuery button#reset': {
				click: this.onQueryResetButtonClick
			},
			'accessQuery button#cancel': {
				click: this.onMinToolClick
			}
        });
        wmf.access = {};
        wmf.access.params = {};
        wmf.access.data = {};
        wmf.access.init = true;
        wmf.access.channelType = '';
    },

    onAddButtonClick: function(){
    	var win = this.getAccessEast();
        win.removeAll();
        win.add(Ext.widget("accessAdd"));
        win.show();
    },
    
    onCellDblClick: function(list, td, cellIndex, record, tr, rowIndex, e, eOpts){
    	var win = this.getAccessEast();
        win.removeAll();
        win.add(Ext.widget("accessEdit"));
        win.show();
        wmf.access.data = record.data;
        if(wmf.access.data.flowControl == -1)
        	wmf.access.data.flowControl = '';
        this.getAccessEdit().getForm().setValues(wmf.access.data);
    },
    
    onQueryButtonClick: function(){
    	var win = this.getAccessEast();
        win.removeAll();
        win.add(Ext.widget("accessQuery"));
        win.show();
        this.getAccessQuery().getForm().setValues(wmf.access.params);
    },
    
    onExportButtonClick: function(){
    	var ecId = wmf.access.params.ecId ? wmf.access.params.ecId : '';
		window.location.href='link/exportLinkList.do?ecId='
			+encodeURI(ecId);
    },
    
    onAddResetButtonClick: function(){
    	this.getAccessAdd().getForm().reset();
    },
    
	onAddSaveButtonClick: function(){
    	var me = this;
    	var former = this.getAccessAdd();
    	var value = former.getValues();
		var win = this.getAccessEast();
		Ext.Ajax.request({
			url:'link/saveLink.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getAccessList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onEditResetButtonClick: function(){
    	var form = this.getAccessEdit().getForm();
    	form.reset();
    	form.setValues(wmf.access.data);
    },
    
	onEditSaveButtonClick: function(){
    	var me = this;
    	var former = this.getAccessEdit();
    	var value = former.getValues();
		var win = this.getAccessEast();
		Ext.Ajax.request({
			url:'link/saveLink.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getAccessList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onQueryResetButtonClick: function(){
    	var form = this.getAccessQuery().getForm();
    	form.reset();
    },
    
	onQuerySaveButtonClick: function(){
    	var me = this;
    	var former = this.getAccessQuery();
    	var value = former.getValues();
		var win = this.getAccessEast();
		wmf.access.init = true;
		wmf.access.params = value;
		win.hide();
		me.getAccessList().getStore().pagebar.getStore().currentPage = 1;
		me.getAccessList().getStore().reload();
    },
	
	onMinToolClick: function(){
    	var win = this.getAccessEast();
    	win.hide();
    },
    
    onBeforeRender: function(component,options){
    	wmf.data.loadAction(this.getAccessMain().itemId, this.getAccessList());
    	wmf.access.params = {};
    	component.getStore().load();
    }
});